import os
import sys
import random
import pyqt5
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QWidget

#建立窗口组件
class DesktopPet(QWidget):
    def __init__(self, parent=None, **kwargs):
        super(DesktopPet, self).__init__(parent)
        self.show()
      #初始化窗口
      self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.SubWindow)
      self.setAutoFillBackground(False)
      self.setAttribute(Qt.WA_TranslucentBackground, True)
      self.repaint()
      self.pet_images, iconpath = self.LoadPetImages()
      #退出选项
      def rightMenu(self,a):
          menu=QMenu(self)
          menu.addAction(QAction('隐藏',self,triggered=self.hide))
          menu.addAction(QAction('退出',self,triggered=self.quit))
          menu.exec_(QCursor.a())
      def quit(self):
          self.close()
          sys.exit()
      def hide(self):
          self.setVisible(False)
      if _name_=='_main_'
        app=QApplocation(sys.argv)
        pet=tablepet()
        sys.exit(app.exec_())
      #鼠标移动
      def mousePressEvent(self, event):
          if event.button() == Qt.LeftButton:
        self.is_follow_mouse = True
        self.mouse_drag_pos = event.globalPos() - self.pos()
        event.accept()
        self.setCursor(QCursor(Qt.OpenHandCursor))

      def mouseMoveEvent(self, event):
          if Qt.LeftButton and self.is_follow_mouse:
        self.move(event.globalPos() - self.mouse_drag_pos)
        event.accept()

      def mouseReleaseEvent(self, event):
          self.is_follow_mouse = False
          self.setCursor(QCursor(Qt.ArrowCursor))

      #计时器静置动作
     def QTimer(self):
      # 每隔一段时间做个动作
          self.timer = QTimer()
          self.timer.timeout.connect(self.randomAct)
          self.timer.start(500)
      #'''随机做一个动作'''
      def randomAct(self):
          if not self.is_running_action:
        self.is_running_action = True
        self.action_images = random.choice(self.pet_images)
        self.action_max_len = len(self.action_images)
        self.action_pointer = 0
        self.runFrame()
      #'''完成动作的每一帧'''
      def runFrame(self):
          if self.action_pointer == self.action_max_len:
              self.is_running_action = False
              self.action_pointer = 0
              self.action_max_len = 0
          self.setImage(self.action_images[self.action_pointer])
          self.action_pointer += 1
#导入图片组
def LoadPetImages(self):
    pet_name = random.choice(list(cfg.PET_ACTIONS_MAP.keys()))
    actions = cfg.PET_ACTIONS_MAP[pet_name]
    pet_images = []
    for action in actions:
        pet_images.append(
            [self.loadImage(os.path.join(cfg.ROOT_DIR, pet_name, 'shime' + item + '.png')) for item in action])
    iconpath = os.path.join(cfg.ROOT_DIR, pet_name, 'shime1.png')
    return pet_images, iconpath






